const config = require('../config.js')
const upload = require('../..//bubaocloud/service/request.js').upload
const setHttp = require('../../bubaocloud/service/request.js').setHttp

const file = {
  uploadFile(data){
    return upload({
      url: `${config.host.rest}/api/File/Upload/${data.fileName}`, 
        filePath: data.path,
        name: `${data.fileName}`,
        header: {
          'Content-Type': 'multipart/form-data',
          'Content-Range': `bytes 0-${data.size - 1}/${data.size}`
        },
      });
  },

  get_fileById(id){
    return setHttp(
        `api/File/${id}`,
        '',
        'GET'
      )
  },

  get_fileDownById(id){
    return setHttp(
        `api/File/Download/${id}`,
        '',
        'GET'
      )
  },

  uploadFiles(files){
    // for (let i = 0; i < files.length; i++) return file.uploadFile();
    let i=0;
    if(i<files.length) {
      
    }
  },

  deleteFile(opt){
    return new Promise((resolve, reject) => {
      setHttp({
        url: config.getUrl(config.API.delete_deleteFile(opt.conf)),
        data: opt.data,
        method: 'DELETE'
      })
      .then(res => { resolve(res) })
      .catch(err => { reject(err) })
      .unsubscribe()
    })
  },
  /**
   * @desc 获取下载地址
   */

  getFileDownLoad(fileId, process = '', videoProcess=''){
    return setHttp(
      `/api/File/Download/${fileId}?process=${process}&videoProcess=${videoProcess}`,
      { fileId},
      'get'
    );
  },

  getRootFolder(opt){
    return new Promise((resolve, reject) => {
      setHttp({
        url: config.getUrl(config.API.get_rootFolder),
        method: 'GET'
      })
      .then(res => { resolve(res) })
      .catch(err => { reject(err) })
      .unsubscribe()
    })
  },

  getFolder(opt){
    return new Promise((resolve, reject) => {
      setHttp({
        url: config.getUrl(config.API.get_folder(opt.conf)),
        method: 'GET'
      })
      .then(res => { resolve(res) })
      .catch(err => { reject(err) })
      .unsubscribe()
    })
  },

  createFolder(opt){
    return new Promise((resolve, reject) => {
      setHttp({
        url: config.getUrl(config.API.put_createFolder(opt.conf)),
        method: 'PUT'
      })
      .then(res => { resolve(res) })
      .catch(err => { reject(err) })
      .unsubscribe()
    })
  },

  deleteRootFolder(opt){
    return new Promise((resolve, reject) => {
      setHttp({
        url: config.getUrl(config.API.delete_deleteRootFolder(opt.conf)),
        method: 'DELETE'
      })
      .then(res => { resolve(res) })
      .catch(err => { reject(err) })
      .unsubscribe()
    })
  },

  deleteFolder(opt){
    return new Promise((resolve, reject) => {
      setHttp({
        url: config.getUrl(config.API.delete_deleteFolder(opt.conf)),
        method: 'DELETE'
      })
      .then(res => { resolve(res) })
      .catch(err => { reject(err) })
      .unsubscribe()
    })
  },

  renameFile(opt){
    return new Promise((resolve, reject) => {
      setHttp({
        url: config.getUrl(config.API.put_renameFile(opt.conf)),
        method: 'PUT'
      })
      .then(res => { resolve(res) })
      .catch(err => { reject(err) })
    })
  },

  getFolderByType(type = 0){
    return new Promise((resolve, reject) => {
      setHttp({
        url: config.getUrl(config.API.folderByType(opt.conf)),
        method: 'GET'
      })
      .then(res => { resolve(res) })
      .catch(err => { reject(err) })
    })
  },

  uploadFileToPath(data){
    return new Promise((resolve, reject) => {
      getFolderByType()
        .then(folder => {
          let opt = {
            conf: {
              fileName: data['fileName'],
              id: folder['TreeId'],
              folderId: folder['FolderId']
            },
            path: data['path'],
            key: data['key']
          }
          uploadFile(opt)
            .then(res => { resolve(res) })
            .catch(err => { resolve(err) })
        })
        .catch(err => { reject(err) })
    })
  }


}

module.exports = file
